package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.model.TbSpecificationOption;
import com.dongdongshop.page.PageParms;
import com.dongdongshop.page.PageUtil;
import com.dongdongshop.service.SpecificationOptionService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecificationOptionVO;
import com.dongdongshop.vo.SpecificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("specifigcation")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;
    @Autowired
    private SpecificationOptionService specificationOptionService;

    @PostMapping("getSpecifigcationList")
    public Result getSpecifigcationList(@RequestBody PageParms<SpecificationVO> page) {
        PageUtil pageUtil = specificationService.getSpecifigcationList(page);
        return Result.OK().setData(pageUtil);
    }

    @PostMapping("addSpecifigcation")
    public Result addSpecifigcation(@RequestBody SpecificationVO specificationVO) {
        specificationService.addSpecifigcation(specificationVO);
        return Result.OK();
    }

    @GetMapping("getSpecifigcationById/{id}")
    public Result getSpecifigcationById(@PathVariable("id") Long id) {
        SpecificationVO specificationVO = specificationService.getSpecifigcationById(id);
        List<SpecificationOptionVO> specificationVOList = specificationOptionService.seleteBySpecId(id);
        specificationVO.setList(specificationVOList);
        return Result.OK().setData(specificationVO);
    }

    @PutMapping("updateSpecifigcation")
    public Result updateBrand(@RequestBody SpecificationVO specificationVO) {
        //String optionListJson = specificationVO.getOptionListJson();
        //List<TbSpecificationOption> list = JSONObject.parseArray(optionListJson, TbSpecificationOption.class);
        //List<SpecificationOptionVO> list = specificationVO.getList();
        specificationService.updatedSpecifigcation(specificationVO);
        return Result.OK();
    }

    @DeleteMapping("deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        specificationService.deleteBatch(ids);
        return Result.OK();
    }

    @PostMapping("getAllSpecification")
    public Result getAllSpecification() {
        List<SpecificationVO> list = specificationService.getAllSpecification();
        return Result.OK().setData(list);
    }
}
